package no.kolstad.movies.dao.hibernate;
/**
 *  Copyright [2008] Christopher Kolstad
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License. 
 */
import java.util.List;

import no.kolstad.movies.dao.BaseDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class BaseDAOHibernate<T> extends HibernateDaoSupport implements
    BaseDAO<T> {
  protected Log log = LogFactory.getLog(this.getClass());

  public void reassociate(T object) {
    getHibernateTemplate().lock(object, LockMode.NONE);
  }

  public void save(T object) {
    getHibernateTemplate().saveOrUpdate(object);
  }

  public void remove(Long id) {
    remove(get(id));
  }

  public void remove(T object) {
    getHibernateTemplate().delete(object);
  }

  public T get(Long id) {
    return getModelClass()
        .cast(getHibernateTemplate().get(getModelClass(), id));
  }

  @SuppressWarnings(value = { "unchecked" })
  public List<T> loadAll() {
    return getHibernateTemplate().loadAll(getModelClass());
  }

  public List<T> findByNamedQueryAndNamedParam(String queryName,
      String paramName, Object param) {
    return findByNamedQueryAndNamedParam(queryName, new String[] { paramName },
        new Object[] { param });
  }

  @SuppressWarnings(value = { "unchecked" })
  public List<T> findByNamedQueryAndNamedParam(String queryName,
      String[] paramNames, Object[] params) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName,
        paramNames, params);
  }

  protected abstract Class<T> getModelClass();
}

